package components.common
{
	import controls.DataHub;
	import controls.events.ServiceEvent;
	
	import data.DegreeType;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.controls.ComboBox;

	public class ProgramCombo extends ComboBox
	{
		private var hub:DataHub;
		
		public function ProgramCombo()
		{
			super();
			hub = DataHub.instance;
			this.labelField = "name";
		}
		
		// ------------------ Type Controlling Functions --------------- //
		private var _type:String;
		
		[Bindable]	[Inspectable(default = MAJORS)]
		public function get type():String
		{
			return _type; 
		}
		public function set type(value:String):void
		{
			_type = value;	
			getContent();
		}
		
		private function getContent(evt:ServiceEvent = null):void
		{
			hub.removeEventListener(ServiceEvent.MAJORS_LOADED, getContent);
			hub.removeEventListener(ServiceEvent.MINORS_LOADED, getContent);
			hub.removeEventListener(ServiceEvent.CERTIFICATES_LOADED, getContent);
			
			switch(type)
			{
				case DegreeType.MAJOR:
					hub.addEventListener(ServiceEvent.MAJORS_LOADED, getContent);
					this.dataProvider = new ArrayCollection(hub.getMajors());
					break;
				case DegreeType.MINOR:
					hub.addEventListener(ServiceEvent.MINORS_LOADED, getContent);
					this.dataProvider = new ArrayCollection(hub.getMinors());
					break;
				case DegreeType.CERTIFICATE:
					hub.addEventListener(ServiceEvent.CERTIFICATES_LOADED, getContent);
					this.dataProvider = new ArrayCollection(hub.getCertificates());
					break;
			}	
			var sort:Sort = new Sort();
			sort.fields = [new SortField("name")];
			(this.dataProvider as ArrayCollection).sort = sort;
			 (this.dataProvider as ArrayCollection).refresh();
		}
		// ------------------ END Type Controlling Functions --------------- //
		
		
	}
}